Goal

Create an interactive plot to explore the distribution of vacant (i.e. boarded up) houses in Baltimore, Maryland, USA.

Prepare Data

Load Baltimore vacant (i.e. boarded up) houses data

balt_vacant <- read.socrata("https://data.baltimorecity.gov/resource/rw5h-nvv4.csv")

Tidy location data

location <- str_split(balt_vacant$location, pattern = " ", simplify = TRUE) %>%
    as_tibble()
colnames(location) <- c("location_type", "longitude", "latitude")

location <- purrr::map_dfc(location,
                      ~ str_remove_all(.x, pattern = "\\(|\\)") %>%
                          str_trim(.)
                      ) %>%
            type_convert(.)

balt_vacant <- cbind(balt_vacant, location)

Load Baltimore neighborhood boundaries

To give someone not familiar with Baltimore some context

neighborhoods <- read_sf("https://data.baltimorecity.gov/resource/h3fx-54q3.geojson")

Complete map

leaflet(data = balt_vacant) %>%
    addTiles(.) %>%
    addHeatmap(., lng = ~ longitude, lat = ~ latitude,
               radius = 10,
               minOpacity = 0.5, max = 100
               ) %>%
    addPolygons(., data = neighborhoods,
                color = "black", weight = 1, opacity = 0.5, fillOpacity = 0,
                label = ~ htmlEscape(label)
                )

Session info

sessionInfo()
## R version 3.5.0 (2018-04-23)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 16299)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] htmltools_0.3.6      sf_0.6-2             leaflet.extras_1.0.0
##  [4] leaflet_2.0.0        forcats_0.3.0        stringr_1.3.0       
##  [7] dplyr_0.7.4          purrr_0.2.4          readr_1.1.1         
## [10] tidyr_0.8.0          tibble_1.4.2         ggplot2_2.2.1       
## [13] tidyverse_1.2.1      RSocrata_1.7.4-7    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.16      lubridate_1.7.4   lattice_0.20-35  
##  [4] class_7.3-14      assertthat_0.2.0  rprojroot_1.3-2  
##  [7] digest_0.6.15     psych_1.8.4       mime_0.5         
## [10] R6_2.2.2          cellranger_1.1.0  plyr_1.8.4       
## [13] backports_1.1.2   e1071_1.6-8       evaluate_0.10.1  
## [16] httr_1.3.1        pillar_1.2.2      rlang_0.2.0      
## [19] curl_3.2          RPostgreSQL_0.6-2 lazyeval_0.2.1   
## [22] readxl_1.1.0      rstudioapi_0.7    rmarkdown_1.9    
## [25] udunits2_0.13     foreign_0.8-70    htmlwidgets_1.2  
## [28] munsell_0.4.3     shiny_1.0.5       broom_0.4.4      
## [31] compiler_3.5.0    httpuv_1.4.2      modelr_0.1.1     
## [34] pkgconfig_2.0.1   mnormt_1.5-5      crayon_1.3.4     
## [37] later_0.7.2       grid_3.5.0        nlme_3.1-137     
## [40] spData_0.2.8.3    jsonlite_1.5      xtable_1.8-2     
## [43] gtable_0.2.0      DBI_1.0.0         magrittr_1.5     
## [46] units_0.5-1       scales_0.5.0      cli_1.0.0        
## [49] stringi_1.1.7     reshape2_1.4.3    promises_1.0.1   
## [52] bindrcpp_0.2.2    xml2_1.2.0        tools_3.5.0      
## [55] glue_1.2.0        hms_0.4.2         crosstalk_1.0.0  
## [58] parallel_3.5.0    yaml_2.1.19       colorspace_1.3-2 
## [61] classInt_0.2-3    rvest_0.3.2       knitr_1.20       
## [64] bindr_0.1.1       haven_1.1.1